package edu.whu.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.*;

@Aspect
@Component
public class ResponseTimeAspect {
    public static Map<String, List<Long>> map = new HashMap<>();

    @Pointcut("execution(* edu.whu.service.*.*(..))")
    private void pointCut(){
    }

    @Around("pointCut()")
    private Object countTime(ProceedingJoinPoint jp) throws Throwable {
        String name = jp.getSignature().toString();
        long t1 = Calendar.getInstance().getTimeInMillis();
        Object retVal = jp.proceed();
        long t2 = Calendar.getInstance().getTimeInMillis();

        if(map.containsKey(name)){
            map.get(name).add(t2-t1);
        }
        else {
            List<Long> times = new ArrayList<>();
            times.add(t2-t1);
            map.put(name,times);
        }
        return retVal;
    }

}
